package net.huashitong.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import jodd.http.HttpRequest;
import jodd.http.HttpResponse;
import net.huashitong.config.WebWeChatConfig;
import net.huashitong.entity.AccessToken;
import net.huashitong.entity.WeChatUserInfo;
import net.huashitong.ssydt.security.utils.Sha1Util;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

/**
 *
 */
public class WebLoginUtils {


    /**
     *  用户同意授权，获取code
     * @param state
     * @return 重定向的URL
     */
    public static String  authorize(String state){
        StringBuffer sb = new StringBuffer();
        sb.append(WebWeChatConfig.Url.AUTHORIZE).append("?");
        sb.append("appid=").append(WebWeChatConfig.APPID);
        try {
            sb.append("&redirect_uri=").append(URLEncoder.encode(WebWeChatConfig.REDIRECT_URI, "utf-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
        sb.append("&response_type=code");
        sb.append("&scope=snsapi_userinfo");
        sb.append("&state=").append(state);
        sb.append("#wechat_redirect");
        return sb.toString();
    }

    /**
     * 第二步：通过code换取网页授权access_token
     * @param code
     * @return
     */
    public static AccessToken getAccessToken(String code){
        StringBuffer sb = new StringBuffer();
        sb.append(WebWeChatConfig.Url.ACCESS_TOKEN).append("?");
        sb.append("appid=").append(WebWeChatConfig.APPID);
        sb.append("&secret=").append(WebWeChatConfig.APPSECRET);
        sb.append("&code=").append(code);
        sb.append("&grant_type=authorization_code");
        HttpRequest request = HttpRequest.get(sb.toString());
        HttpResponse response = request.send();
        return JSON.parseObject(response.bodyText(), AccessToken.class);
    }

    /**
     * 拉取用户信息(需scope为 snsapi_userinfo)
     * @param access_token
     * @param openid
     * @return
     */
    public static WeChatUserInfo getUserInfo(String access_token, String openid){
        StringBuffer sb = new StringBuffer();
        sb.append(WebWeChatConfig.Url.USERINFO).append("?");
        sb.append("access_token=").append(access_token);
        sb.append("&openid=").append(openid);
        sb.append("&lang=zh_CN ");
        HttpRequest request = HttpRequest.get(sb.toString());
        HttpResponse response = request.send();
        response.statusCode();
        return JSON.parseObject(response.bodyText(), WeChatUserInfo.class);
    }

    public static JSONObject getJsapi_ticket(String access_token) {
        StringBuffer sb = new StringBuffer();
        sb.append("https://api.weixin.qq.com/cgi-bin/ticket/getticket?");
        sb.append("access_token=").append(access_token);
        sb.append("&type=jsapi");
        HttpRequest request = HttpRequest.get(sb.toString());
        HttpResponse response = request.send();

        return JSON.parseObject(response.bodyText());
    }

    public static String getJsapi_sign(String ticket,String url,String nonceStr,String timestamp) {
        StringBuffer sb = new StringBuffer();
        sb.append("jsapi_ticket=").append(ticket);
        sb.append("&noncestr=").append(nonceStr);
        sb.append("&timestamp=").append(timestamp);
        sb.append("&url=").append(url);

        return Sha1Util.getSha1(sb.toString());

    }

    public static AccessToken getAccessToken() {
        StringBuffer sb = new StringBuffer();
        sb.append("https://api.weixin.qq.com/cgi-bin/token?");
        sb.append("grant_type=client_credential");
        sb.append("&appid=").append(WebWeChatConfig.APPID);
        sb.append("&secret=").append(WebWeChatConfig.APPSECRET);
        HttpRequest request = HttpRequest.get(sb.toString());
        HttpResponse response = request.send();

        return JSON.parseObject(response.bodyText(),AccessToken.class);
    }
}
